package com.imy.lua.dialog;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.imy.common.base.BaseDialog;
import com.imy.common.base.BaseKeyboardDialog;
import com.imy.common.base.GeneralDialog;

import com.imy.common.constant.SPFlagCache;
import com.imy.common.util.SPUtil;
import com.imy.common.util.ToastUtil;
import com.imy.lua.R;



public class ColorGetDialog extends BaseKeyboardDialog {
    private EditText xEt;
    private EditText yEt;
    private TextView manualTv;
    private TextView confirmTv;
    public ColorGetDialog(AppCompatActivity activity) {
        super(activity);
    }

    @Override
    protected void init(View view) {
        super.init(view);
        xEt =view.findViewById(R.id.x_et);
        yEt=view.findViewById(R.id.y_et);
        manualTv=view.findViewById(R.id.manual_tv);
        confirmTv=view.findViewById(R.id.confirm_tv);
        manualTv.setOnClickListener(v -> {
            dismiss();
            //弹出一个提示
            boolean aBoolean = SPUtil.getBoolean(SPFlagCache.GET_COLOR_TIPS, false);
            if (!aBoolean){
                GeneralDialog generalDialog = GeneralDialog.
                        newInstance((AppCompatActivity) mActivity, null, "按住并滑动到需要取色的坐标点，" +
                                        "松开即可取到该点的颜色", null, "不再提示",
                                "确定");
                generalDialog.show();
                generalDialog.getLeftTv().setOnClickListener(v1 -> {
                    SPUtil.setBoolean(SPFlagCache.GET_COLOR_TIPS,true);
                    generalDialog.dismiss();
                    if (listener!=null){
                        listener.onManualColorGet();
                    }
                });
                generalDialog.getRightTv().setOnClickListener(v12 -> {
                    generalDialog.dismiss();
                    if (listener!=null){
                        listener.onManualColorGet();
                    }
                });
            }else {
                if (listener!=null){
                    listener.onManualColorGet();
                }
            }
        });
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener!=null){
                    String s = xEt.getText().toString();
                    String s1 = yEt.getText().toString();
                    if (TextUtils.isEmpty(s) || TextUtils.isEmpty(s1)){
                        ToastUtil.toast(mActivity,"xy参数错误");
                        return;
                    }
                    listener.onGetXYColor(Integer.parseInt(s),Integer.parseInt(s1));
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_color_get;
    }

    public interface ColorGetListener{
        void  onManualColorGet();
        void  onGetXYColor(int x,int y);
    }
    public void setColorGetListener(ColorGetListener listener){
        this.listener= listener;
    }
    private ColorGetListener listener;
}
